//
//  SLDeployRecomViewModel.m
//  StarLight
//
//  Created by 吴涛 on 16/6/5.
//  Copyright © 2016年 Duke.Wu. All rights reserved.
//

#import "SLDeployRecomViewModel.h"
#import "SLNetWork.h"
#import "SLNearLyShip.h"



@interface SLDeployRecomViewModel ()

@property (nonatomic, strong) NSNumber *pageNum;

@property (nonatomic, strong) NSString *requstPath;

@property (nonatomic, strong) NSString *gid;

@property (nonatomic, strong) NSString *bid;

@end



@implementation SLDeployRecomViewModel

- (instancetype)initWithBid:(NSString *)bid{
  if (self = [super init]) {
    self.type = 0;
    [self addObservers];
    [self getRecomGoodsWithBid:bid];
  }
  return self;
}


- (instancetype)initWithGid:(NSString *)gid{
  if (self = [super init]) {
    self.type = 1;
    [self addObservers];
    [self getRecomBoatWithGid:gid];
  }
  return self;
}


- (void)addObservers{
  
  @weakify(self)
  
  [[RACObserve(self, pageNum) filter:^BOOL(id value) {
    return value != nil;
  }] subscribeNext:^(NSNumber *page) {
    @strongify(self)
    NSString *path;
    if (self.type == 0) {
      path = [NSString stringWithFormat:@"%@/goods/recomlist?bid=%@&page=%@",baseUrl,self.bid,page];
    }else{
      path = [NSString stringWithFormat:@"%@/boat/recomlist?gid=%@&page=%@",baseUrl,self.gid,page];
    }
    self.requstPath = path;
  }];
  
  [[RACObserve(self, requstPath) filter:^BOOL(id value) {
    return value != nil;
  }] subscribeNext:^(NSString *path) {
  
    @strongify(self)
    [SLNetWork getDataWithRequest:path complete:^(id respond, NSError *error) {
      if(!error){
       
        if (self.type == 0) {
          NSMutableArray *arr = [SLNearLyProduct mj_objectArrayWithKeyValuesArray:respond[@"data"]];
          self.recomList = arr;
        }else{
          NSMutableArray *arr = [SLNearLyShip mj_objectArrayWithKeyValuesArray:respond[@"data"]];
          self.recomList = arr;
        }
        
      }
    }];
  }];
  
}




#pragma mark - Private Methods

/**
 *  根据货源id 获取推荐船舶
 *
 *  @param gid 货源id
 */
- (void)getRecomBoatWithGid:(NSString *)gid{
  self.gid = gid;
  self.requstPath = [NSString stringWithFormat:@"%@/boat/recomlist?gid=%@&page=%@",baseUrl,gid,@1];

}

/**
 *  根据船舶id 获取推荐货源
 *
 *  @param bid 船舶id
 */
- (void)getRecomGoodsWithBid:(NSString *)bid{
  
  self.bid = bid;
  self.requstPath = [NSString stringWithFormat:@"%@/goods/recomlist?bid=%@&page=%@",baseUrl,bid,@1];
  
}

#pragma mark - Public Methods

- (void)deleteShipWithBid:(NSString *)bid block:(DeleRecomBlock)block{
  NSString *postUrl = [NSString backUrlWithService:@"boat" methodName:@"emptyboats"];
  NSDictionary *para = @{@"gid":self.gid, @"bids":bid};
  [self deleteRecomendWithPostUrl:postUrl parameters:para block:^{
    block();
  }];
}

-(void)deleteGoodsWithGid:(NSString *)gid block:(DeleRecomBlock)block{
  
  NSString *postUrl = [NSString backUrlWithService:@"goods" methodName:@"emptygoods"];
  NSDictionary *para = @{@"bid":self.bid, @"gids":gid};
  [self deleteRecomendWithPostUrl:postUrl parameters:para block:^{
    block();
  }];
}

- (void)deleteRecomendWithPostUrl:(NSString *)url parameters:(NSDictionary *)para block:(DeleRecomBlock)block{
  [SVProgressHUD show];
  
  [SLNetWork postDataWithParameters:para postUrl:url complete:^(id respond, NSError *error) {
    if (!error) {
      block();
    }
  }];
}

- (void)deleteAllShipWithGid:(NSString *)gid block:(DeleRecomBlock)block{
  
  NSString *postUrl = [NSString backUrlWithService:@"boat" methodName:@"emptyallboats"];
  NSDictionary *para = @{@"gid":gid,@"uid":[SLNetWork backUid]};
  [self deleteRecomendWithPostUrl:postUrl parameters:para block:^{
    block();
  }];

}
- (void)deleteAllGoodsWithbid:(NSString *)bid block:(DeleRecomBlock)block{
  NSString *postUrl = [NSString backUrlWithService:@"goods" methodName:@"emptyallgoods"];
  NSDictionary *para = @{@"bid":bid,@"uid":[SLNetWork backUid]};
  
  [self deleteRecomendWithPostUrl:postUrl parameters:para block:^{
    block();
  }];
}



- (void)next{
  self.pageNum = @(self.pageNum.integerValue + 1);
}

- (void)first{
  self.pageNum = @1;
}






@end
